package com.hducms.cms.action.front;


import java.sql.Timestamp;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;

import com.hducms.cms.entity.front.ContentListEntity;
import com.hducms.cms.entity.front.ContentM;
import com.hducms.cms.entity.front.ContentItem;
import com.hducms.cms.entity.front.ContentResponse;
import com.hducms.cms.entity.main.CmsChecked;
import com.hducms.cms.entity.main.CmsUser;
import com.hducms.cms.entity.main.Content;
import com.hducms.cms.service.CmsCheckedService;
import com.hducms.cms.service.CmsCommentService;
import com.hducms.cms.service.CmsUserExtService;
import com.hducms.cms.service.ContentService;
import com.hducms.cms.web.CmsUtils;
import com.hducms.common.page.Pagination;
import com.hducms.common.util.AES;
import com.hducms.common.util.ClientUtils;
import com.hducms.common.util.DateFormatUtils;

@Controller
public class ContentActM {

	@SuppressWarnings("unchecked")
	@RequestMapping(value="/content/list.m")
	public void getContentList(Integer cid, Integer pno, 
			HttpServletResponse response,HttpServletRequest request,String publickey) {
		// 获取已经通过的内容列表
		if (null == pno) {
			pno = 1;
		}
		List<Content> list = new ArrayList<Content>();
		Pagination page = manager.getPageForMember(null, cid, null, pno, 5);
		list = (List<Content>) page.getList();
		List<ContentItem> listM = new ArrayList<ContentItem>();

		for (Content l : list) {
			ContentItem m = new ContentItem();
			m.setId(l.getId());
			m.setT(l.getTitle());
			m.setDate( DateFormatUtils.gettDateString(l.getDate()) );
			listM.add(m);
		}
		ContentListEntity entiy = new ContentListEntity();
		entiy.setItems(listM);
		if (!listM.isEmpty()) {
			entiy.setNextPage(page.getNextPage());
		}

		ClientUtils.resultResponse(response, entiy,publickey);
	}
	
	@RequestMapping(value="/content/detail.m")
	public void getContentDetail(Integer cid, HttpServletResponse response,
			HttpServletRequest request,String publickey) {
		
		//获取用户id
		CmsUser user = CmsUtils.getUser(request);
		Integer uid = user.getUserId();
		
		// 获取id获取内容
		Content content = manager.findById(cid);
		ContentM c = new ContentM();
		int commentCount=0;
		commentCount = commentService.getTotalCount(cid);
		int isChecked = 0;
		isChecked = cmsCheckedService.isChecked(cid,uid);
		if (content!=null) {
			c.setAuthor(content.getChannel().getName());
			c.setData(DateFormatUtils.gettDateString(content.getDate()));
			c.setId(content.getId());
			c.setT(content.getTitle());
			try {
				c.setTxt(AES.decrypt(content.getTitle(), content.getTxt()));
			} catch (Exception e) {
				System.out.println("解密失败！");
				c.setTxt(content.getTxt());
			}
			c.setCommentCount(commentCount);
			c.setIsread(isChecked);
		}
		
//		ClientUtils.resultResponse(response, c,publickey);
		ClientUtils.resultResponse(response, new ContentResponse(c),publickey);

	}
	
	@RequestMapping(value="/content/readCheck.m")
	public void readChecked(Integer contentId,
			HttpServletRequest request,HttpServletResponse response,String publickey) {

		CmsUser user = CmsUtils.getUser(request);
		Integer userId = user.getUserId();
		Date date = new Date();
		date = new Timestamp(System.currentTimeMillis());
		CmsChecked cmsChecked = new CmsChecked();
		cmsChecked.setUserId(userId);
		cmsChecked.setContentId(contentId);
		cmsChecked.setReadDate(date);
		int isRead=0;
		isRead = cmsCheckedService.isChecked(userId, contentId);
		if (isRead==1) {
			ClientUtils.resultResponse(response, 1,publickey); //已评论
			return;
		}
		else {
			cmsCheckedService.save(cmsChecked);
			ClientUtils.resultResponse(response, 1,publickey); //阅读确认成功返回1
			return;
		}
	}
	
	@Autowired
	private ContentService manager;
	@Autowired
	private CmsCommentService commentService;
	@Autowired
	private CmsUserExtService userExtService;
	@Autowired
	private CmsCheckedService cmsCheckedService;
}


